package com.changgou.order.listener;

import com.alibaba.fastjson.JSON;
import com.changgou.order.service.OrderService;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Map;

/**
 * 描述
 *
 * @author www.itheima.com
 * @version 1.0
 * @package com.changgou.order.listener *
 * @since 1.0
 */
@Component
@RabbitListener(queues = "${mq.pay.queue.order}")
public class OrderUpdateListener {

    @Autowired
    private OrderService orderService;

    @RabbitHandler
    public void handlerData(String msg) {
//将数据转成Map
        Map<String, String> result = JSON.parseObject(msg, Map.class);

        //return_code=SUCCESS
        String return_code = result.get("return_code");
        //业务结果
        String result_code = result.get("result_code");

        System.out.println("return_code="+return_code);
        System.out.println("result_code="+result_code);

        //业务结果 result_code=SUCCESS/FAIL，修改订单状态
        if (return_code.equalsIgnoreCase("success")) {
            //获取订单号
            String outtradeno = result.get("out_trade_no");
            //业务结果
            if (result_code.equalsIgnoreCase("success")) {
                if (outtradeno != null) {
                    //修改订单状态  out_trade_no
                    orderService.updateStatus(outtradeno, result.get("transaction_id"));
                }
            } else {
                //订单删除
                orderService.deleteOrder(outtradeno);
            }
        }
    }
}
